package com.lihepeng.leecode.tree;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你一个二叉搜索树的根节点 root ，返回 树中任意两不同节点值之间的最小差值 。
 * @ main.java.leecode.tree.Solution530
 *
 */
public class Solution783 {
    // 二叉搜索树 是 有序的，所以中序遍历的结果是 一个有序的数组
    List<Integer> params = new ArrayList<>();
    public int minDiffInBST(TreeNode root) {
        inOrderTravel(root);
        int minVal = Integer.MAX_VALUE;
        for (int i=0;i<params.size()-1;i++){
            int leftVal = params.get(i);
            int rightVal = params.get(i+1);
            minVal = Math.min(minVal,Math.abs(rightVal - leftVal));
        }
        return minVal;

    }
    private void inOrderTravel(TreeNode root){
        if (root == null){
            return ;
        }
        inOrderTravel(root.left);
        params.add(root.val);
        inOrderTravel(root.right);
    }
    @Test
    public void runTest(){
        TreeNode root = new TreeNode(4);
        TreeNode l1 = new TreeNode(2);
        TreeNode l2 = new TreeNode(6);
        TreeNode m1 = new TreeNode(1);
        TreeNode m2 = new TreeNode(3);
        root.left = l1;
        root.right = l2;
        l1.left = m1;
        l1.right = m2;
        int i = minDiffInBST(root);
        System.out.println(i);
    }
}
